package com.tenqian.media.controller;

import com.tenqian.Result;
import com.tenqian.media.domain.Carousel;
import com.tenqian.media.service.CarouselService;
import com.tenqian.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

@Controller
@RequestMapping("/carousel")
public class CarouselController {
    Logger logger = LoggerFactory.getLogger(CarouselController.class);
    @Autowired
    CarouselService carouselService;

    @RequestMapping("/findAll")
    @ResponseBody
    public Result findAllCarousel() {
        return ResultUtil.success(carouselService.findAll());
    }

    @RequestMapping("/addAll")
    @ResponseBody
    public Result addAll(HttpServletRequest request) {
        String realPath = request.getSession().getServletContext().getRealPath("/resources/carousel");
        File file = new File(realPath);
        File[] files = file.listFiles();
        for (File file1 : files) {
            String path = file1.getPath();
            if (carouselService.findByPath(path.substring(path.indexOf("/resources"))) != null) continue;
            Carousel carousel = new Carousel();
            carousel.setPath(path.substring(path.indexOf("/resources")));
            carousel.setDescs("lalalalalalalalalalala");
            carouselService.save(carousel);
        }
        return ResultUtil.success();
    }

}
